package com.hehui.partimejob.activity.ques;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.WebView;

import androidx.lifecycle.Observer;

import com.google.gson.Gson;
import com.hehui.partimejob.BaseActivity;
import com.hehui.partimejob.R;
import com.hehui.partimejob.js.ques.EditQuesJSFace;
import com.hehui.partimejob.js.JSBean.QuesItem;
import com.hehui.partimejob.uitls.LiveDataBus;
import com.hehui.partimejob.view.BaseWebView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class EditQuesActivity extends BaseActivity {
    private WebView wv;
    private List<QuesItem> data;


    @Override
    public int getLayoutId() {
        return R.layout.activity_editqest;
    }

    @Override
    public void init() {
        getNavView().hide();
        wv = (WebView) findViewById(R.id.wv);
        data = new ArrayList<>();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseWebView baseWebView = new BaseWebView(wv);
        EditQuesJSFace editQuesJSFace = new EditQuesJSFace(A);
        Intent intent = getIntent();
        String title = intent.getStringExtra("title");
        String welcome = intent.getStringExtra("welcome");
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("title", title);
        stringObjectHashMap.put("welcome", welcome);
        editQuesJSFace.setData(stringObjectHashMap);
        baseWebView.addJavascriptInterface(editQuesJSFace);
        baseWebView.loadFromAsset("quesEdit.html");

        LiveDataBus.getInstance().with("head", ArrayList.class).observe(this, new Observer<ArrayList>() {
            @Override
            public void onChanged(ArrayList s) {
                if (s.size() != 0) {
                    baseWebView.executeJsFunction("app.setHead", s.get(0), s.get(1));
                }
            }
        });

        LiveDataBus.getInstance().with("end", String.class).observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                if(!TextUtils.isEmpty(s)){
                    baseWebView.executeJsFunction("app.setFoot",s);
                }
            }
        });

        LiveDataBus.getInstance().with("quesData", QuesItem.class).observe(this, new Observer<QuesItem>() {
            @Override
            public void onChanged(QuesItem quesItem) {
//                data.add(quesItem);
                String s = new Gson().toJson(quesItem);
                Log.d(TAG, "onChanged: " + s);
                baseWebView.executeJsFunction("app.getItem",  s);

            }
        });
    }
}
